package cn.lds.j368.dsbank.service.impl;

import cn.lds.j368.dsbank.constant.DsBankConstants;
import cn.lds.j368.dsbank.domain.EmpRole;
import cn.lds.j368.dsbank.domain.Employee;
import cn.lds.j368.dsbank.dto.RespDTO;
import cn.lds.j368.dsbank.exception.ServiceException;
import cn.lds.j368.dsbank.mapper.EmployeeMapper;
import cn.lds.j368.dsbank.service.EmpRoleService;
import cn.lds.j368.dsbank.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

import java.util.stream.Stream;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmpRoleService empRoleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    private Logger log = LoggerFactory.getLogger(EmpRoleService.class);

    @Override
    public RespDTO<List<Employee>> selectEmpAll() throws ServiceException {

        List<Employee> employees = employeeMapper.selectEmpAll();

        if (CollectionUtils.isEmpty(employees)){
            throw new ServiceException(DsBankConstants.EmpMsg.QUERY_EMP_FAIL);
        }


        return RespDTO.buildSuccess(employees);
    }

    @Override
    public RespDTO<Employee> selectEmpByEmpId(Integer empId) throws ServiceException {

        Employee employee = employeeMapper.selectEmpById(empId);

        if (null == employee){
            throw new ServiceException(DsBankConstants.EmpMsg.QUERY_EMP_FAIL);
        }

        return RespDTO.buildSuccess(employee);
    }

    @Override
    public RespDTO addEmp(Employee employee,Integer... roles) throws ServiceException {

        //定义初始默认密码
        employee.setEmpPassword("0000");

        //密码加密
        employee.setEmpPassword(passwordEncoder.encode(employee.getEmpPassword()));

        //添加员工,mybaits-plus默认添加数据后实体类对象返回新增的主键id
        int insert = employeeMapper.insert(employee);
        //添加员工角色
        Stream.of(roles).forEach(rId -> {
            empRoleService.addEmpRole(new EmpRole(employee.getEmpId(),rId));
        });

        //为员工添加角色信息

        if (insert <=0){
            throw new ServiceException(DsBankConstants.SysMsg.ADD_DATA_FAIL);
        }


        return RespDTO.buildSuccess();
    }

    @Override
    public RespDTO modifyEmp(Employee employee) throws ServiceException {

        int i = -1;

        //对密码进行处理，首先根据id查询到原始的员工数据
        Employee oldEmployee = employeeMapper.selectEmpById(employee.getEmpId());
        //如果密码没修改
        if (oldEmployee.getEmpPassword().equals(employee.getEmpPassword())){
            i = employeeMapper.updateById(employee);
        }else {
            //如果修改过密码，对新密码加密
            employee.setEmpPassword(passwordEncoder.encode(employee.getEmpPassword()));
            i = employeeMapper.updateById(employee);
        }


        if (i < 0){
            throw  new ServiceException(DsBankConstants.SysMsg.MODIFY_DATA_FAIL);
        }

        return RespDTO.buildSuccess();
    }

    @Override
    public RespDTO delEmpById(Integer empId) throws ServiceException {

        int i = employeeMapper.deleteById(empId);

        if (i <= 0){
            throw new ServiceException(DsBankConstants.SysMsg.DEL_DATA_FAIL);
        }

        return RespDTO.buildSuccess();
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Employee employee = employeeMapper.selectEmpByEmpUsername(s);

        log.info("登录员工：{}",employee.getAuthorities());

        return employee;
    }
}
